package main

import (
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"fmt"
)


/*
Host: https://mercury-uat.phonepe.com

Merchant ID: M2306160483220675579140 (unless stated otherwise)

Keys: The below mentioned keys will be used for authentication.

Key Index	Key
1	8289e078-be0b-484d-ae60-052f117f8deb
2	48b7228c-605e-4bab-9632-5bf9e61e8b75
4	8eab4e0e-111c-42f6-940c-cae648688141
5	5be04f9c-535f-408b-aebd-72cbf91c72c1
6	b972ea5a-84d8-4399-89c3-63c6df0d6e86
7	3864e1a6-b13c-4058-a63e-24ea4e2baf85
8	73377ac5-b167-4e41-bb74-8524a2cb39c5


{
  "request": "eyAgCiAgICJtZXJjaGFudElkIjoiTTIzMDYxNjA0ODMyMjA2NzU1NzkxNDAiLAogICAidHJhbnNhY3Rpb25JZCI6IlRYMTIzNDU2Nzg5IiwKICAgIm1lcmNoYW50VXNlcklkIjoiVTEyMzQ1Njc4OSIsCiAgICJhbW91bnQiOjEwMCwKICAgIm1lcmNoYW50T3JkZXJJZCI6Ik9EMTIzNCIsCiAgICJtb2JpbGVOdW1iZXIiOiI5eHh4eHh4eHh4IiwKICAgIm1lc3NhZ2UiOiJwYXltZW50IGZvciBvcmRlciBwbGFjZWQgT0QxMjM0IiwKICAgInN1Yk1lcmNoYW50IjoiRGVtb01lcmNoYW50IiwKICAgImVtYWlsIjoiYW1pdCoqKjc1QGdtYWlsLmNvbSIsCiAgICJzaG9ydE5hbWUiOiJBbWl0Igp9"
}

eyAgCiAgICJtZXJjaGFudElkIjoiTTIzMDYxNjA0ODMyMjA2NzU1NzkxNDAiLAogICAidHJhbnNhY3Rpb25JZCI6IlRYMTIzNDU2Nzg5IiwKICAgIm1lcmNoYW50VXNlcklkIjoiVTEyMzQ1Njc4OSIsCiAgICJhbW91bnQiOjEwMCwKICAgIm1lcmNoYW50T3JkZXJJZCI6Ik9EMTIzNCIsCiAgICJtb2JpbGVOdW1iZXIiOiI5eHh4eHh4eHh4IiwKICAgIm1lc3NhZ2UiOiJwYXltZW50IGZvciBvcmRlciBwbGFjZWQgT0QxMjM0IiwKICAgInN1Yk1lcmNoYW50IjoiRGVtb01lcmNoYW50IiwKICAgImVtYWlsIjoiYW1pdCoqKjc1QGdtYWlsLmNvbSIsCiAgICJzaG9ydE5hbWUiOiJBbWl0Igp9
eyAgCiAgICJtZXJjaGFudElkIjoiTTIzMDYxNjA0ODMyMjA2NzU1NzkxNDAiLAogICAidHJhbnNhY3Rpb25JZCI6IlRYMTIzNDU2Nzg5IiwKICAgIm1lcmNoYW50VXNlcklkIjoiVTEyMzQ1Njc4OSIsCiAgICJhbW91bnQiOjEwMCwKICAgIm1lcmNoYW50T3JkZXJJZCI6Ik9EMTIzNCIsCiAgICJtb2JpbGVOdW1iZXIiOiI5eHh4eHh4eHh4IiwKICAgIm1lc3NhZ2UiOiJwYXltZW50IGZvciBvcmRlciBwbGFjZWQgT0QxMjM0IiwKICAgInN1Yk1lcmNoYW50IjoiRGVtb01lcmNoYW50IiwKICAgImVtYWlsIjoiYW1pdCoqKjc1QGdtYWlsLmNvbSIsCiAgICJzaG9ydE5hbWUiOiJBbWl0Igp9

 */

func base64Req() string{
	str := "{  \n   \"merchantId\":\"M2306160483220675579140\",\n   \"transactionId\":\"TX123456789\",\n   \"merchantUserId\":\"U123456789\",\n   \"amount\":100,\n   \"merchantOrderId\":\"OD1234\",\n   \"mobileNumber\":\"9xxxxxxxxx\",\n   \"message\":\"payment for order placed OD1234\",\n   \"subMerchant\":\"DemoMerchant\",\n   \"email\":\"amit***75@gmail.com\",\n   \"shortName\":\"Amit\"\n}"

	str64 := base64.StdEncoding.EncodeToString([]byte(str))

	//fmt.Println(str64)
	return str64
}

func xverify() string{
	//SHA256(base64 encoded payload + "/v4/debit" + salt key) + ### + salt index
	str64 := base64Req()
	saltIndex := "2"
	saltKey := "48b7228c-605e-4bab-9632-5bf9e61e8b75"
	str := str64 + "/v4/debit" + saltKey
	hash := sha256.New()
	hash.Write([]byte(str))
	hashString := hex.EncodeToString(hash.Sum(nil))
	return hashString + "###" + saltIndex
}

func test1(){
	str := "{\"status\":\"SUCCESS\", \n\"message\":\"Token generated\", \n\"subCode\":\"200\", \n\"data\": {\"token\":\"eyJ0eXA...fWStg\", \"expiry\":1564130052}\n}"
	mp := map[string]interface{}{}
	json.Unmarshal([]byte(str), &mp)
	fmt.Println(mp, mp["data"])
	data := mp["data"].(map[string]interface{})
	fmt.Println(data["token"])
}

func maptest(mp map[string]string){
	ss, err := json.Marshal(mp)
	fmt.Println(mp["ss"], string(ss), err)
	mp2 := map[string]string{}
	json.Unmarshal(ss, &mp2)
	fmt.Println(mp2)
}

func main()  {
	fmt.Println("________test__")
	base64Req()
	fmt.Println(xverify())

	test1()
	maptest(nil)
}

